﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;


namespace WLC
{
    class WLCAdjustWorkloadsStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new WLCAdjustWorkloadsStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
        
            IPropertyDefinition systemOrderID = schema.AddExpressionProperty("SystemOrderID", "WLCOrder.ID");
            systemOrderID.Description = "The order id used by Simio (WLCOrder.ID).";
            systemOrderID.Required = true;

            IPropertyDefinition sequence = schema.AddElementProperty("OrderSequence", WLCSequenceElementDefinition.ID);
            sequence.Description = "The sequence of this order.";
            sequence.Required = true;

            IPropertyDefinition loadAccount = schema.AddElementProperty("LoadAccount", WLCLoadAccountElementDefinition.ID);
            loadAccount.Description = "The load account to adjust the load on.";
            loadAccount.Required = true;


        }

        public string Name
        {
            get { return "AdjustWorkloads"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }

        public int NumberOfExits
        {
            get { return 1; }
        }

      
        public Guid UniqueID
        {
            get { return new Guid("{3C883F60-2702-11DF-9CEE-2B5756D89593}"); }
        }

        #endregion
    }


    class WLCAdjustWorkloadsStep : IStep
    {
        IElementProperty _SequenceProp;
        IElementProperty _loadAccountProp;
        IPropertyReader _systemOrderIDProp;


        public WLCAdjustWorkloadsStep(IPropertyReaders bag)
        {
            _systemOrderIDProp = bag.GetProperty("SystemOrderID");
            _SequenceProp = (IElementProperty) bag.GetProperty("OrderSequence");
            _loadAccountProp = (IElementProperty)bag.GetProperty("LoadAccount");
        }

        #region IStep Members

        public ExitType Execute(IStepExecutionContext context)
        {
            WLCSequenceElement sequence = (WLCSequenceElement) _SequenceProp.GetElement(context);
            
                        

            WLCLoadAccountElement loadAccount = (WLCLoadAccountElement) _loadAccountProp.GetElement(context);

            int systemOrderID = (int) _systemOrderIDProp.GetDoubleValue(context);

            double orderSize = (double)context.AssociatedObject.States["OrderSize"].StateValue;

            // The load account element takes care of updating the workloads depending
            // on the configured value for the workload method
            loadAccount.AdjustWorkloads(systemOrderID, orderSize, sequence);

            return ExitType.FirstExit;
        }

        #endregion

    }

}


